﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class EditLoanPhone : BasePage
    {
        int id = 0;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            
            mode = string.IsNullOrEmpty(Request.QueryString[MODE]) ? ADD_MODE : Request.QueryString[MODE];
            if (mode == ADD_MODE)
                btnSave.Enabled = false;

            if (!string.IsNullOrEmpty(Request.QueryString[ID]))
            {
                id = int.Parse(Request.QueryString[ID]);

                if (!IsPostBack && mode != ADD_MODE)
                {
                    LoadInfo();
                }
            }
        }

        protected void LoadInfo()
        {
            POSSystem.BLL.LoanPhones loanPhone = new POSSystem.BLL.LoanPhones();
            loanPhone.LoadByPrimaryKey(id);
            if (loanPhone.RowCount > 0)
            {
                editLoanPhone.IMEI = loanPhone.s_IMEI;
                editLoanPhone.BrandID = loanPhone.BrandID;
                editLoanPhone.ProductName = loanPhone.s_ProductName;
            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                Save();
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true); 
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("EditLoanPhone.aspx.cs", "btnSave_OnClick", ex.StackTrace);
            }
        }

        protected void btnSaveAndClose_OnClick(object sender, EventArgs e)
        {
            try
            {
                Save();
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};window.close();", 1), true); 
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("EditLoanPhone.aspx.cs", "btnSave_OnClick", ex.StackTrace);
            }
        }

        protected void Save()
        {
            POSSystem.BLL.LoanPhones loanPhone = new POSSystem.BLL.LoanPhones();
            if (mode == ADD_MODE)
            {
                loanPhone.AddNew();
                loanPhone.BrandID = Branches.CurrentBranchID;
                loanPhone.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                loanPhone.Deleted = false;
                loanPhone.Free = true;
                loanPhone.BranchID = Branches.CurrentBranchID;
            }
            else
            {
                loanPhone.LoadByPrimaryKey(id);
                if (loanPhone.RowCount <= 0)
                    return;
            }

            loanPhone.ProductName = editLoanPhone.ProductName;
            loanPhone.BrandID = editLoanPhone.BrandID;

            loanPhone.IMEI = editLoanPhone.IMEI;

            loanPhone.Save();
        }
    }
}